package com.hudson.admin.controller;


import com.hudson.admin.config.LoginConfig;
import com.hudson.admin.domain.LoginDomain;
import com.hudson.admin.domain.Result;
import com.hudson.admin.interceptor.UserContext;
import com.hudson.admin.util.JwtUtils;
import com.hudson.admin.util.Results;
import com.hudson.core.util.StringUtil;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


/**
 * @Author: 渔小鲤
 * @DateTime: 2025/7/21 21:20
 **/
@RestController
@RequestMapping("/user")
public class UserController {
    @Resource
    private LoginConfig loginConfig;
    @Resource
    private JwtUtils jwtUtils;

    @PostMapping("/login")
    public Result<String> login(@RequestBody LoginDomain param) {
        if (loginConfig.getUserName().equals(param.getUserName()) && loginConfig.getPassword().equals(param.getPassword())) {
            String token = jwtUtils.generateToken(param.getUserName());
            return Results.success("成功登录", token);
        }
        return Results.error("登录失败", null);
    }

    @GetMapping("/validate-token")
    public Result<Boolean> validateToken() {
        return Results.success(StringUtil.noEmpty(UserContext.getUserName()));
    }
}
